package com.kudian.restful.controller;

import com.kudian.common.persistence.ResultObj;
import com.kudian.common.utils.StringUtils;
import com.kudian.restful.entity.Cart;
import com.kudian.restful.entity.LoginSession;
import com.kudian.restful.service.CartService;
import com.kudian.restful.service.LoginSessionService;
import com.kudian.restful.service.UsersService;
import com.kudian.restful.vo.cart.*;
import com.wordnik.swagger.annotations.ApiOperation;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by Administrator on 2016/4/27.
 */
@RestController
@RequestMapping("cart")
public class CartController {

    private static final Logger logger = Logger.getLogger(CartController.class);

    @Autowired
    private CartService cartService;

    @Autowired
    private LoginSessionService loginSessionService;

    @Autowired
    private UsersService usersService;

//    @RequestMapping(value = "addToCart", method = RequestMethod.POST)
//    @ApiOperation(value = "添加产品到购物车", httpMethod = "POST", notes = "添加产品到购物车", response = ResultObj.class)
//    public @ResponseBody ResultObj addToCart(@RequestBody AddCartInfoVO vo) {
//
//        ResultObj o = new ResultObj();
//        if (vo == null || StringUtils.isBlank(vo.getAccess_token())) {
//            o.setErrcode(9999);
//            o.setErrmsg("添加产品到购物车失败!access_token为空");
//            return o;
//        }
//        LoginSession ent = loginSessionService.getInfo(vo.getAccess_token());
//        if (ent == null) {
//            o.setErrcode(9999);
//            o.setErrmsg("access_token已失效!");
//            return o;
//        }
//
//        try {
//            int ret = usersService.checkSellerUser(vo.getAccess_token());
//            if (ret == -1) {
//                throw new Exception("用户数据异常!");
//            }
//            if (1 == ret) {
//                o.setErrcode(1100);
//                o.setErrmsg("商家用户目前未开通购买商品功能");
//                return o;
//            }
//
//            if (0 == ret) {
//                cartService.addToCart(vo);
//                o.setErrcode(0);
//                o.setErrmsg("添加产品到购物车成功");
//            }
//
//        } catch (Exception e) {
//            o.setErrcode(9999);
//            o.setErrmsg("添加产品到购物车失败");
//            logger.error(e);
//        }
//        return o;
//    }

    @RequestMapping(value = "addOneToCart", method = RequestMethod.POST)
    @ApiOperation(value = "购物车中产品加一", httpMethod = "POST", notes = "购物车中产品加一", response = AddOneToCartRetVO.class)
    public @ResponseBody AddOneToCartRetVO addOneToCart(@RequestBody AddOneToCartVO vo) {
        AddOneToCartRetVO o = new AddOneToCartRetVO();
        if (vo == null || StringUtils.isBlank(vo.getAccess_token())) {
            o.setErrcode(9999);
            o.setErrmsg("添加产品到购物车失败!access_token为空");
            return o;
        }

        try {
            LoginSession ent = loginSessionService.getInfo(vo.getAccess_token());
            if (ent == null) {
                o.setErrcode(9999);
                o.setErrmsg("access_token已失效!");
                return o;
            }

            Cart cart = cartService.addOneToCart(vo);
            o.setSellerId(cart.getSellerId());
            o.setGoodsId(cart.getGoodsId());
            o.setGoodsNumber(cart.getGoodsNumber());
            o.setErrmsg("成功加1");
            o.setErrcode(0);
        } catch (Exception e) {
            o.setErrcode(9999);
            o.setErrmsg("加1失败");
            logger.error(e);
        }
        return o;
    }

    @RequestMapping(value = "subOneToCart", method = RequestMethod.POST)
    @ApiOperation(value = "购物车中产品减一", httpMethod = "POST", notes = "购物车中产品减一", response = ResultObj.class)
    public @ResponseBody SubOneToCartRetVO subOneToCart(@RequestBody SubOneToCartVO vo) {
        SubOneToCartRetVO o = new SubOneToCartRetVO();
        if (vo == null || StringUtils.isBlank(vo.getAccess_token())) {
            o.setErrcode(9999);
            o.setErrmsg("添加产品到购物车失败!access_token为空");
            return o;
        }

        try {
            LoginSession ent = loginSessionService.getInfo(vo.getAccess_token());
            if (ent == null) {
                o.setErrcode(9999);
                o.setErrmsg("access_token已失效!");
                return o;
            }

            Cart cart = cartService.subOneToCart(vo);
            if (cart == null) {
                o.setErrmsg("购物车中已无此商品");
                o.setErrcode(0);
            } else {
                o.setSellerId(cart.getSellerId());
                o.setGoodsId(cart.getGoodsId());
                o.setGoodsNumber(cart.getGoodsNumber());
                o.setErrmsg("成功减1");
                o.setErrcode(0);
            }
        } catch (Exception e) {
            o.setErrcode(9999);
            o.setErrmsg("减1失败");
            logger.error(e);
        }
        return o;
    }

    @RequestMapping(value = "queryCartInfo", method = RequestMethod.POST)
    @ApiOperation(value = "取得当前登录用户中的购物车信息", httpMethod = "POST", notes = "取得当前登录用户中的购物车信息", response = QueryCartInfoRetVO.class)
    public @ResponseBody QueryCartInfoRetVO queryCartInfo(@RequestBody QueryCartInfoVO vo) {

        QueryCartInfoRetVO o = new QueryCartInfoRetVO();
        if (vo == null || StringUtils.isBlank(vo.getAccess_token())) {
            o.setErrcode(9999);
            o.setErrmsg("取得当前登录用户中的购物车信息失败!access_token为空");
            return o;
        }

        try {
            LoginSession ent = loginSessionService.get(vo.getAccess_token());
            if (ent == null) {
                o.setErrcode(9999);
                o.setErrmsg("access_token已失效!");
                return o;
            }

            o.setCarts(cartService.queryCartInfo(vo.getAccess_token(), null));
            o.setErrcode(0);
            o.setErrmsg("取得当前登录用户中的购物车信息成功");
        } catch (Exception e) {
            o.setErrcode(9999);
            o.setErrmsg("取得当前登录用户中的购物车信息失败");
            logger.error(e);
        }
        return o;
    }

    @RequestMapping(value = "queryCartInfoBySeller", method = RequestMethod.POST)
    @ApiOperation(value = "取得当前登录用户某一商家购物车信息", httpMethod = "POST", notes = "取得当前登录用户某一商家购物车信息", response = QueryCartInfoRetVO.class)
    public @ResponseBody QueryCartInfoRetVO queryCartInfoBySeller(@RequestBody QueryCartInfoBySellerVO vo) {

        QueryCartInfoRetVO o = new QueryCartInfoRetVO();
        if (vo == null || StringUtils.isBlank(vo.getAccess_token())) {
            o.setErrcode(9999);
            o.setErrmsg("取得当前登录用户某一商家购物车信息失败!access_token为空");
            return o;
        }

        try {
            LoginSession ent = loginSessionService.get(vo.getAccess_token());
            if (ent == null) {
                o.setErrcode(9999);
                o.setErrmsg("access_token已失效!");
                return o;
            }

            o.setCarts(cartService.queryCartInfo(vo.getAccess_token(), vo.getSellerId()));
            o.setErrcode(0);
            o.setErrmsg("取得当前登录用户某一商家购物车信息成功");
        } catch (Exception e) {
            o.setErrcode(9999);
            o.setErrmsg("取得当前登录用户某一商家购物车信息失败");
            logger.error(e);
        }
        return o;
    }

    @RequestMapping(value = "clearCart", method = RequestMethod.POST)
    @ApiOperation(value = "清空购物车信息", httpMethod = "POST", notes = "清空购物车信息", response = ResultObj.class)
    public ResultObj clearCart(@RequestBody ClearCartVO vo) {
        ResultObj o = new ResultObj();
        if (vo == null || StringUtils.isBlank(vo.getAccess_token())) {
            o.setErrcode(9999);
            o.setErrmsg("清空购物车信息失败!access_token为空");
            return o;
        }

        try {
            LoginSession ent = loginSessionService.getInfo(vo.getAccess_token());
            if (ent == null) {
                o.setErrcode(9999);
                o.setErrmsg("access_token已失效!");
                return o;
            }

            cartService.clearCart(vo);
            o.setErrcode(0);
            o.setErrmsg("清空购物车信息成功");
        } catch (Exception e) {
            o.setErrcode(9999);
            o.setErrmsg("清空购物车信息失败");
            logger.error(e);
        }
        return o;
    }
}
